﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1 {
    class ConsoleHandler {
        private ConsoleColor basicForegroundColor;
        private ConsoleColor basicBackgroundColor;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="f">Standard Foreground Color</param>
        /// <param name="b">Standard Background Color</param>
        public ConsoleHandler(String f, String b) {
            basicForegroundColor = getColor(f, 'f');
            basicBackgroundColor = getColor(b, 'b');
        }


        /// <summary>
        /// Writes message in the console with colors &lt;$foreground&gt;&lt;#background&gt; and goes to next line
        /// </summary>
        /// <param name="message">The message that needs to be put on the screen</param>
        public void writeLine(String message = "") {
            write(message);
            Console.Write("\n\r");
        }

        /// <summary>
        /// Writes message in the console with colors &lt;$foreground&gt;&lt;#background&gt;
        /// </summary>
        /// <param name="message">The message that needs to be put on the screen</param>
        public void write(String message) {
            //Set basic colors
            setForegroundColor();
            setBackgroundColor();

            //set variables
            bool isColor = false;
            bool isForeground = false;
            bool isBackground = false;
            String color = "";

            //loop through whole message
            foreach (char c in message) {
                if (!isColor) {
                    if (c.Equals('<')) {                         // Starttag color
                        isColor = true;
                    } else {
                        Console.Write(c);
                    }
                } else {
                    if (c.Equals('>')) {                         //Stoptag color
                        if (color.Length > 0) {                  //when we found some color
                            isColor = false;
                            if (isForeground) {                  //is color foreground?
                                setForegroundColor(color);
                                isForeground = false;
                            } else if (isBackground) {           //is color background?
                                setBackgroundColor(color);
                                isBackground = false;
                            }
                            color = "";
                        }
                    } else if (isForeground || isBackground) {   //add char to color string
                        color += c;
                    } else {
                        if (c.Equals('T')) {                     // Tag foreground color
                            isForeground = true;
                        } else if (c.Equals('B')) {              // Tag background color
                            isBackground = true;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Read an normal string with all characters accepted
        /// </summary>
        /// <param name="foregroundColor">The text color</param>
        /// <param name="backgroundColor">The background color</param>
        public String read(String foregroundColor = null, String backgroundColor = null) {
            setForegroundColor(foregroundColor);
            setBackgroundColor(backgroundColor);
            return Console.ReadLine();
        }

        public int readInt(int min = -1, int max = -1) {
            //Set basic colors
            setForegroundColor();
            setBackgroundColor();

            //set variables
            int temp = -1;
            bool useMinimal = false;
            bool useBoth = false;
            String input = "";


            if (min != -1) {
                if (max != -1) {        //with minimal and maximal
                    useBoth = true;
                } else {                //only minimal
                    useMinimal = true;
                }
            }
            for (; ; ) {
                try {
                    input = Console.ReadLine();
                    temp = int.Parse(input);
                    if (useMinimal) {
                        if (temp >= min) {
                            break;
                        }
                    } else if (useBoth) {
                        if (temp >= min && temp <= max) {
                            break;
                        }
                    } else if (temp > -1) {
                        break;
                    }
                    resetLine();
                } catch (Exception) {
                    resetLine();
                }
            }

            return temp;
        }

        public void clearScreen() {
            Console.Clear();
        }

        public void resetLine() {
            int line = Console.CursorTop;
            //set cursor naar begin van de regel
            Console.SetCursorPosition(0, Console.CursorTop - 1); // 1 regel omhoog
            //loop door de input string (char voor char)
            for (; ; ) {
                //replace with space
                Console.Write(' ');
                if (Console.CursorTop == line) {
                    break;
                }
            }
            //set cursor naar begin van de regel
            Console.SetCursorPosition(0, Console.CursorTop - 1); // niet 1 regel omhoog
        }

        internal void resetDoubleLine() {
            resetLine();
            resetLine();
        }

        /// <summary>
        /// Finds the correct ConsoleColor by the String color :
        /// white,black,red,blue,green,yellow,gray or cyan
        /// </summary>
        /// <param name="color">The full name of the color</param>
        /// <returns>ConsoleColor</returns>
        private ConsoleColor getColor(String color, char i) {
            switch (color) {
                case "white":
                    return ConsoleColor.White;
                case "black":
                    return ConsoleColor.Black;
                case "red":
                    return ConsoleColor.Red;
                case "blue":
                    return ConsoleColor.Blue;
                case "green":
                    return ConsoleColor.Green;
                case "yellow":
                    return ConsoleColor.Yellow;
                case "gray":
                    return ConsoleColor.Gray;
                case "cyan":
                    return ConsoleColor.Cyan;
                case "dblue":
                    return ConsoleColor.DarkBlue;
                case "dgreen":
                    return ConsoleColor.DarkGreen;
                case "dred":
                    return ConsoleColor.DarkRed;
                case "reset":
                    if (i == 'f') {
                        return basicForegroundColor;
                    } else {
                        return basicBackgroundColor;
                    }
                default:
                    return ConsoleColor.White;
            }
        }

        public void setForegroundColor(string foregroundColor = null) {
            if (foregroundColor == null) {
                Console.ForegroundColor = basicForegroundColor;
            } else {
                Console.ForegroundColor = getColor(foregroundColor, 'f');
            }
        }

        public void setBackgroundColor(string backgroundColor = null) {
            if (backgroundColor == null) {
                Console.BackgroundColor = basicBackgroundColor;
            } else {
                Console.BackgroundColor = getColor(backgroundColor, 'b');
            }
        }
    }
}
